package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.mapper.BuildingMapper;
import com.yiyang.mapper.BuildingRoomMapper;
import com.yiyang.mapper.RoomMapper;
import com.yiyang.pojo.BuildingRoom;
import com.yiyang.pojo.vo.BuildingRoomVO;
import com.yiyang.pojo.vo.PageResult;
import com.yiyang.pojo.vo.Result;
import com.yiyang.service.BuildingRoomService;
import com.yiyang.utils.BizCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BuildingRoomServiceImpl extends ServiceImpl<BuildingRoomMapper, BuildingRoom> implements BuildingRoomService {
    @Autowired
    private BuildingRoomMapper buildingRoomMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public Result<BuildingRoom> getBuildingRoomById(Integer id) {
        BuildingRoom byId = this.getById(id);
        // 需要再去查询楼宇和房间的信息，需要封装楼宇的名称和房间的编号数据
        if(byId != null){
            BuildingRoomVO buildingRoomVO = new BuildingRoomVO();
            buildingRoomVO.setBuildingName(this.buildingMapper.selectById(byId.getId()).getName());
            buildingRoomVO.setRoomNum(this.roomMapper.selectById(byId.getId()).getNum());
            // 使用工具类，实现两个对象之间的属性数据的拷贝操作
            BeanUtils.copyProperties(byId,buildingRoomVO);
            return Result.success(BizCode.SUCCESS,buildingRoomVO);
        }
        return Result.fail(BizCode.NO_DATA);
    }

    @Override
    public Result<PageResult<List<BuildingRoom>>> getAllBuildingRoom(Integer currentPage, Integer pageSize, String type, String description) {
        Page<BuildingRoom> page = new Page<>(currentPage,pageSize);
        QueryWrapper<BuildingRoom> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(type),"type",type)
                .like(StringUtils.isNotBlank(description),"description",description);
        this.buildingRoomMapper.selectPage(page,wrapper);
        if(page.getRecords().size() > 0){
            PageResult<List<BuildingRoom>> pageResult = new PageResult<>();
            pageResult.setTotal(page.getTotal());
            pageResult.setData(page.getRecords());
            return Result.success(BizCode.SUCCESS);
        }else {
            return Result.fail(BizCode.NO_DATA);
        }
    }

    @Override
    public Result<Void> addBuildingRoom(BuildingRoom buildingRoom) {
        if(buildingRoom.getIsDelete() == null){
            buildingRoom.setIsDelete(0);
        }
        if(this.save(buildingRoom)){
            return Result.success();
        }
        return Result.fail(BizCode.ADD_BUILDING_ROOM_ERROR);
    }

    @Override
    public Result<Void> updateBuildingRoom(BuildingRoom buildingRoom) {
        if(this.updateById(buildingRoom)){
            return Result.success();
        }
        return Result.fail(BizCode.UPDATE_BUILDING_ROOM_ERROR);
    }

    @Override
    public Result<Void> deleteBuildingRoom(Integer id) {
        if(this.removeById(id)){
            return Result.success();
        }
        return Result.fail();
    }
}
